<?php

namespace App\Http\Controllers\Admin;

use App\Admin;
use App\Logic\Log\LogOperation;
use App\Models\Store;
use App\Repositories\CityRepository;
use Illuminate\Http\Request;
use App\Common\CommonHelpers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\Repositories\RoleRepository;
use App\Repositories\UserRepository;

class AdminController extends Controller
{
    /**
     * @var UserRepository
     */
    protected $repository;
    protected static $logmodel=null;
//    /**
//     * UsersController constructor.
//     *
//     * @param UserValidator $validator
//     */
    public function __construct(UserRepository $repository)
    {
       // self::$logmodel=new LogOperation();
        if (method_exists($this, 'repository')) {
            $this->repository = $this->repository();
        } else {
            $this->repository = $repository;
        }
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = Admin::all();
        if (request()->wantsJson()) {
            return response()->json([
                'code' => 0,
                'msg' => '',
                'data' => $users,
            ]);
        }
        return view('admin.admin.users.index');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function create(Request $request)
    {

        if ($request->wantsJson()) {
            $user = $this->repository->create($request->all());
            if (!$user) {
                return response()->json([
                    'code' => 1001,
                    'msg' => '操作失败'
                ]);
            }
            return response()->json([
                'code' => 0,
                'msg' => '操作成功'
            ]);
        }
        return view('admin.admin.users.form');
    }

    /**
     *  Update a created resource in storage.
     */
    public function update(Request $request, $id)
    {
        if ($request->wantsJson()) {
            $user = $this->repository->update($request->all(), $id);
            if (!$user) {
                return response()->json([
                    'code' => 1001,
                    'msg' => '操作失败'
                ]);
            }
            return response()->json([
                'code' => 0,
                'msg' => '操作成功'
            ]);
        }
        $user = $this->repository->find($id);
        return view('admin.admin.users.form', compact('user'));
    }

    public function relate(RoleRepository $roleRepository, $id)
    {
        if(request()->isMethod('post')) {
            $roles = request()->only('roles');
            if (!$roles) {
                return response()->json([
                    'code' => 1001,
                    'msg' => '请选择角色'
                ]);
            }
            if (!$this->repository->relate($roles, $id)) {
                return response()->json([
                    'code' => 1001,
                    'msg' => '操作失败'
                ]);
            }
            return response()->json([
                'code' => 0,
                'msg' => '操作成功'
            ]);
        }
        $roles = $roleRepository->format();
        $related = $this->repository->related($id);
        return view('admin.admin.users.relate', compact('roles', 'related'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if (request()->wantsJson()) {
            $user_name=Admin::where('id',$id)->first()->name;

            $deleted = $this->repository->delete($id);
            if (!$deleted) {
                return response()->json([
                    'code' => 1001,
                    'msg' => '操作失败'
                ]);
            }

            self::$logmodel->create(auth()->guard('admin')->user()->id,'admin','destroy',$user_name.' 用户被注销');

            return response()->json([
                'code' => 0,
                'msg' => '操作成功'
            ]);
        }
    }

    public function resetPassword(Request $request)
    {
        $who = config('scaffolding.auth.guard');
        $user = auth()->guard()->$who();
        if ($request->isMethod('post')) {
            $repassword = $request->input('repassword');
            if ($request->input('confirm_repassword') != $repassword) {
                return response()->json([
                    'code' => '1001',
                    'msg' => '确认新密码与新密码不一致'
                ]);
            }
            if (!$this->repository->resetPassword($user->id, $repassword)) {
                return response()->json([
                    'code' => '1002',
                    'msg' => '操作失败'
                ]);
            }
            return response()->json([
                'code' => '0',
                'msg' => '操作成功'
            ]);
        }
        return view('admin.admin.users.resetpassword')->with('username', $user->name);
    }

    public function city(CityRepository $cityRepository, $id)
    {
        if(request()->isMethod('post')) {
            $cities = request()->only('cities');
            if (!$cities) {
                return response()->json([
                    'code' => 1001,
                    'msg' => '请选择盟市'
                ]);
            }
            if (! $cityRepository->relate($cities, $id)) {
                return response()->json([
                    'code' => 1001,
                    'msg' => '操作失败'
                ]);
            }
            return response()->json([
                'code' => 0,
                'msg' => '操作成功'
            ]);
        }
        $cities = $cityRepository->format();
        $related = $this->repository->cities($id);
        return view('admin.admin.users.city', compact('cities', 'related'));
    }

}
